<?php

	/**
	 * Workspace@ Engine application comment!
	 * 
	 * LICENSE
	 * This application is licensed under New BSD license.
	 * Please read complete LICENSE.txt inside of root folder for more details.
	 * 
	 * We wish you good building :) 
	 * 
	 * @category   Application
	 * @package    Bootstrap
	 * @author     Nevio Vesic <nevio@workspaceat.com>
	 * @copyright  2008-2009 Workspace@ group in association with Design strategist
	 */

	class Engine_Application_Locator{
		
		/**
		 * Backend url
		 *
		 * @var string
		 */
		protected static $backend_url = null;
		
		/**
		 * Backend access uri
		 *
		 * @var string
		 */
		protected static $backend_access_uri = null;
		
		/**
		 * Frontend url
		 *
		 * @var string
		 */
		protected static $frontend_url = null;
		
		/**
		 * Frontend access uri
		 *
		 * @var string
		 */
		protected static $frontend_access_uri = null;
		
		/**
		 * Location data
		 * 
		 * instance
		 * groups ( array )
		 * file uri
		 *
		 * @var array()
		 */
		protected static $data                = array();
		
		/**
		 * Backend uri data... used when current user accessed backend
		 * This will be returned as manipulation uri elements and not $_SERVER['REQUEST_URI'].
		 * $_R['R_U']; is returned only if current application trying to instanciate frontend!
		 *
		 * @var array()
		 */
		protected static $uri_bdata           = array();
		protected static $uri_params          = array();
		
		/**
		 * Generate front and backend access params
		 *
		 * @param Zend_Config $data
		 */
		function __construct( Zend_Config $data ) 
		{
			
			// BACKEND
			
			if ( self::$backend_url )
			{
				self::$backend_url = null;
			}
			
			self::$backend_url = $data -> backend -> url;
			
			if ( self::$backend_access_uri )
			{
				self::$backend_access_uri = null;
			}
			
			self::$backend_access_uri = $data -> backend -> accessuri;
			
			// FRONTEND
			
			if ( self::$frontend_url )
			{
				self::$frontend_url = null;
			}
			
			self::$frontend_url = $data -> frontend -> url;
			
			if ( self::$frontend_access_uri )
			{
				self::$frontend_access_uri = null;
			}
			
			self::$frontend_access_uri = $data -> frontend -> accessuri;
			
		}
		
		/**
		 * Findout what you need from uri and set current groups, 
		 * instance and file which needs to be loaded!
		 *
		 */
		public function analyzeLocation()
		{
			if ( self::$data )
			{
				self::$data = array();
			}
			
			# If its backend ... than do next shit...
			if ( $this->isBackend() )
			{
				self::$data = array ( 
					"instance" => "backend",
					"elements" => self::$uri_bdata,
					"params"   => self::$uri_params 
				);
				
				return self::$data;
			}
			
			$uri = split ( "/", $_SERVER['REQUEST_URI'] );
			$huri = split ( "/", self::$frontend_access_uri );
			
			foreach ( $uri as $key )
			{
				if (! in_array ( $key, $huri ) && $key !== "" && !eregi(":", $key) )
				{
					$v_uri[] = $key;
					
				}else if (! in_array ( $key, $huri ) && $key !== "" && eregi(":", $key) )
				{
					$v_params[] = str_replace( ":", "", $key );
				}
				
			}
			print_r ( $v_params );
			
			if (! @$v_uri[0] )
			{
				$v_uri = false;
			}
			
			self::$data = array (
				"instance" => "frontend",
				"elements" => $v_uri,
				"params"   => $v_params
			);
			
			return self::$data;
		}
		
		/**
		 * Try to find better solution.. this one sucks totaly..
		 * Sorry folks lazy to change for now.. it's working so...
		 * If anyone want to change it... I will appriciate that :) hehe
		 *
		 * @return bool[true|false]
		 */
		protected function isBackend()
		{
			$domain = $this->getDomain();
			
			if ( self::$backend_url == $domain )
			{
				
				# ok we have same domain name...
				# lets go now to attempt see if uri-s are same
				
				$huri = split ( "/", self::$backend_access_uri );
				$curi = split ( "/", $_SERVER['REQUEST_URI'] );
				$i = 0;
				
				foreach ( $huri as $key )
				{
					if ( $key !== "" )
					{
						$a[] = $key;
						++$i;
					}
				}
				
				foreach ( $curi as $keys )
				{
					if ( $keys !== "" )
					{
						$ba[] = $keys;
					}
				}
				
				for( $b = 0; $b <= $i; $b++ )
				{
					if ( $b !== $i && $b !== 0 )
					{
						$buri .= $curi[$b] . "/";
					}
					else 
					{
						$buri .= $curi[$b];	 
					}
				}
				
				$tdomain = self::$backend_url . self::$backend_access_uri;
				$cdomain = $domain . $buri;
				
				if ( $tdomain == $cdomain )
				{
					if ( self::$uri_bdata )
					{
						self::$uri_bdata = array();
					}
					
					if ( self::$uri_params )
					{
						self::$uri_bdata = array();
					}

					foreach ( $ba as $keys )
					{
						if (! in_array ( $keys, $a ) && !eregi(":", $keys) )
						{
							self::$uri_bdata[] = $keys;
							
						}elseif ( ! in_array ( $keys, $a ) && eregi(":", $keys)  )
						{
							self::$uri_params[] = str_replace( ":", "", $keys );
						}
					}
					
					return true;
				}
				
				return false;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * Get current fully qualified domain name ( with http or https and end / )
		 *
		 * @return string
		 */
		protected function getDomain()
		{
			# ok we need to find out exact number of elements 
			# so that we can see if they are same..
			$a = split( "/",  $_SERVER['SERVER_PROTOCOL'] );
			
			$c_url = null;
			
			if ( $a[0] == "HTTP" )
			{
				$c_url = "http://";
			}
			else if ( $a[0] == "HTTPS" )
			{
				$c_url = "https://";
			}
			
			return $c_url .= $_SERVER['HTTP_HOST'] . "/";
		}
		
		/**
		 * Get current application base instance ( backend or frontend )
		 *
		 * @return string[backend|frontend]
		 */
		public function getApplicationEnviroment()
		{
			return self::$data["instance"];
		}
		
		/**
		 * Return location data
		 * array (
		 *  instance, elements
		 * )
		 *
		 * @return array()
		 */
		public function getLocationData()
		{
			return self::$data;
		}
		
		public function setLocationElements( Array $data )
		{
			return self::$data['elements'] = $data;
		}

	}